package com.srs.dao.agent;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.agent.ChangeServingCell;
import com.srs.util.HibernateUtil;

public class ChangeServingCellDao {

	final static Logger logger = LoggerFactory.getLogger(ChangeServingCellDao.class);

	private List<ChangeServingCell> changeServingCellList;
	private ChangeServingCell changeServingCell;

	public void delete(Integer id) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			changeServingCell = (ChangeServingCell) session.get(ChangeServingCell.class, id);
			session.delete(changeServingCell);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<ChangeServingCell> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			changeServingCellList = session.createQuery("from ChangeServingCell csc").list();
			return changeServingCellList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<ChangeServingCell> list(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from ChangeServingCell csc where csc.onDate=:onDate");
			q.setString("onDate", onDate);
			changeServingCellList = q.list();
			return changeServingCellList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}				
	
	
	@SuppressWarnings("unchecked")
	public List<Integer> listAgentIds(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("select csc.agentId from ChangeServingCell csc where csc.onDate=:onDate");
			q.setString("onDate", onDate);
			List<Integer> listAgentIds = q.list();
			return listAgentIds;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}
	}	

	@SuppressWarnings("unchecked")
	public List<Integer> listAgentIds() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("select csc.agentId from ChangeServingCell csc");
			List<Integer> listAgentIds = q.list();
			return listAgentIds;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}
	}	

	public Long count(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("select count(csc.agentId) from ChangeServingCell csc where csc.onDate=:onDate");
			q.setString("onDate", onDate);
			return (Long) q.uniqueResult();
		} finally {
			session.close();
		}

	}	
	
	public Long count(String onDate, String agentList) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("select count(distinct csc.agentId) from ChangeServingCell csc where csc.onDate=:onDate  and csc.agentId in " + agentList);
			q.setString("onDate", onDate);
			return (Long) q.uniqueResult();
		} finally {
			session.close();
		}

	}	
	
	public ChangeServingCell get(Integer id) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User u where u.uid=:id");
			q.setInteger("userid", id);
			return (ChangeServingCell) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public void insert(ChangeServingCell usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(ChangeServingCell usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
